#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: report_package.py 
@version:
@time: 2022/04/02 
"""
import datetime as datetime

from TapUtil.Tfeishu.feishuReport.report_base import ReportBase
from datetime import datetime

debug_url = 'https://open.feishu.cn/open-apis/bot/v2/hook/ace5b858-0ef6-466f-8b61-cf77a9d23bda'
feishu_send = 'https://open.feishu.cn/open-apis/bot/v2/hook/a7aa2856-a735-4f1a-87de-59a1e6708abe'


class ReportPackage(ReportBase):
    color = ("Green", "blue", "yellow", "red")
    title = {
        'start': ' 打包 start ',
        'finish': ' 打包 finish ',
    }
    message = {}

    def __init__(self):
        ...

    def template_edit_start(self, info):
        build_number = info.get('build_number')
        change_list = info.get('change_list')
        branch_par = info.get('branch_par')
        build_url = info.get('build_url')
        project = info.get('project_name')
        user = info.get('user')
        title = project + self.title.get('start') + build_number
        color = self.color[1]
        self.message = {
            "msg_type": "interactive",
            "card": {
                "header": {
                    "title": {
                        "content": title,
                        "tag": "plain_text"
                    },
                    "template": color
                },
                "config": {
                    "wide_screen_mode": True,
                    "enable_forward": True
                },
                "elements": [
                    {
                        "tag": "div",
                        "text": {
                            "content": f"打包开始时间 " + datetime.now().strftime("%Y-%m-%d  %H:%M:%S") + '\n'
                                                                                                    f'创建人：{user}',
                            "tag": "lark_md"
                        }
                    },
                    {
                        "tag": "div",
                        "text": {
                            "content": " Branch Info:\n" + branch_par,
                            "tag": "lark_md"
                        }
                    },
                    {
                        "tag": "div",
                        "text": {
                            "content": " Change List:\n" + change_list,
                            "tag": "lark_md"
                        }
                    },
                    {
                        "actions": [
                            {
                                "tag": "button",
                                "text": {
                                    "content": f"{build_number} 本次构建",
                                    "tag": "lark_md"
                                },
                                "url": build_url,
                                "type": "default",
                                "value": {}
                            }
                        ],
                        "tag": "action"
                    },
                ]

            }
        }

    def template_edit_finish(self, info):
        build_number = info.get("build_number")
        change_list = info.get("change_list")
        branch_par = info.get("branch_par")
        image_tag = info.get("images")
        status = info.get("status")
        build_url = info.get('build_url')
        project = info.get('project_name')
        user = info.get('user')
        at_user = info.get('at')
        open_setting = {
            # "admin": "all",
            "niko": "ou_1795d7e85afcf8306de4b79a1bd810a3",
            "robot": "ou_2dae753e032882829028b1984e0bd3d4",
            "jackin": "ou_32f33f281866d5a97e3d02cafbd46206",
            "sam": "ou_e3805077a871050e9bfb88669223a82d",
            "lily": "ou_71d788f76f643cc7a54df6542c59f4f2",
            "david": 'ou_0d4b96135423f3643d3760441ba83f05',
            "steven": 'ou_d7bab7cb0fa113179ad5b53dc61bb014',
            "zed": 'ou_567a4cc2c2e783a49e436e060f29aa3c',
            "will": 'ou_c545b9af42859a670171f9acef078e88',
            'feynman': 'ou_de3f574d162d446aa07aecfc671790f8',
            'harsen': 'ou_afb58ba6caf5e7b5e350a792ccfbbbfb',
            'dexter': 'ou_99c53fc8f9fc725a3db5f4f05d09f842',
            'jacques': 'ou_073da79b132974bef12763c20cef2ff8',
            'fannie': 'ou_0212f4c11d3a75e87c6cd4f4722c4349',
        }
        # 打包发起人
        open_id = open_setting.get(user)
        at_people = f'\n<at id={open_id}>打包发起人</at>'
        if open_id is None:
            at_people = f'\n创建人： {user}'
        # 打包关注人
        at_concern_id = ''
        if at_user:
            at_concern_id = open_setting.get('at_user')
            if at_concern_id is None:
                at_concern_id = ''
            if at_concern_id:
                f'\n<at id={at_concern_id}>打包关注人</at>'
        title = project + self.title.get('finish') + build_number
        color = ""
        if status == "SUCCESS":
            color = self.color[0]
        elif status == "ABORTED":
            color = self.color[2]
        elif status == "FAILED":
            color = self.color[3]
        self.message = {
            "msg_type": "interactive",
            "card": {
                "header": {
                    "title": {
                        "content": title,
                        "tag": "plain_text"
                    },
                    "template": color
                },
                "config": {
                    "wide_screen_mode": True,
                    "enable_forward": True
                },
                "elements": [
                    {
                        "tag": "div",
                        "text": {
                            "content": "打包结束时间 " + datetime.now().strftime(
                                "%Y-%m-%d  %H:%M:%S") + at_people + at_concern_id,
                            "tag": "lark_md"
                        }
                    },
                    {
                        "tag": "div",
                        "text": {
                            "content": f'Build Status: {status}\n'
                                       f'\n'
                                       f'Docker Images TAG: \n'
                                       f'\n'
                                       f'{image_tag}'
                            ,
                            "tag": "lark_md"
                        }
                    },
                    {
                        "tag": "div",
                        "text": {
                            "content": f" Branch Info:\n{branch_par}",
                            "tag": "lark_md"
                        }
                    },
                    {
                        "tag": "div",
                        "text": {
                            "content": " Change List:\n"
                                       f"{change_list}\n"
                                       f""""""
                            ,
                            "tag": "lark_md"
                        }
                    },
                    {
                        "actions": [
                            {
                                "tag": "button",
                                "text": {
                                    "content": f"{build_number} 本次构建",
                                    "tag": "lark_md"
                                },
                                "url": build_url,
                                "type": "default",
                                "value": {}
                            }
                        ],
                        "tag": "action"
                    },
                ]
            }
        }

    def report(self):
        self.send(feishu_send, self.message)
        self.message = {}


if __name__ == '__main__':
    T = ReportPackage()
    start = {
        'project_name': 'test',
        'build_number': '1',
        'change_list': '2',
        'branch_par': '3',
        'build_url': '4',
        'user': 'jackin'
    }
    finish = {
        'project_name': 'test',
        'build_number': '1',
        'change_list': '2',
        'branch_par': '3',
        'build_url': '4',
        'images': '5',
        'status': 'FAILED',
        'user': 'admin'
    }
    # T.template_edit_start(start)
    T.template_edit_finish(finish)
    T.report()
